package cs.ui250.carnatic;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class MainActivity extends Activity {

	Context mContext;
	DBAdapter mDB;
	SoundManager mSoundManager;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mContext = getApplicationContext();
		SharedPreferences values = PreferenceManager
				.getDefaultSharedPreferences(mContext);
		mDB = DBAdapter.getInstance(mContext);
		mDB.open();
		mSoundManager = SoundManager.getInstance(mContext);
		if (!values.getBoolean("DB_MADE", false)) {
			importText(mContext);
			SharedPreferences.Editor edit = values.edit();
			edit.putBoolean("DB_MADE", true);
			edit.commit();
		}
		Button mShrutiButton = (Button) findViewById(R.id.main_shruti_button);
		mShrutiButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						ShrutiBoxActivity.class);
				startActivity(intent);
			}
		});
		Button mRagaIdButton = (Button) findViewById(R.id.main_raga_id_button);
		mRagaIdButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						RagaLookUpTabActivity.class);
				startActivity(intent);
			}
		});
		Button mFlashCardsButton = (Button) findViewById(R.id.main_flash_cards_button);
		mFlashCardsButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						FlashCardsActivity.class);
				startActivity(intent);
			}
		});
	}

	public void importText(Context c) {
		InputStream is = c.getResources().openRawResource(R.raw.data);
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		String lineIn = null;
		String[] separated;
		Raga raga;
		Song song;

		try {
			lineIn = reader.readLine(); // get first line before entering parse
										// loop
			while ((lineIn = reader.readLine()) != null) {

				if (!lineIn.equals("*")) {
					separated = lineIn.split(" ");
					raga = new Raga(Integer.parseInt(separated[0]),
							separated[1], Integer.parseInt(separated[2]),
							Integer.parseInt(separated[3]),
							Integer.parseInt(separated[4]),
							Integer.parseInt(separated[5]),
							Integer.parseInt(separated[6]),
							Integer.parseInt(separated[7]),
							Integer.parseInt(separated[8]));
					lineIn = reader.readLine();

					while (!lineIn.equals("*")) {
						String songs = "";
						songs += lineIn + "**"; // title of song
						lineIn = reader.readLine();
						songs += lineIn + "**"; // composer of song
						lineIn = reader.readLine();
						songs += lineIn + "|"; // youtube link to song
						raga.addSongs(songs);
						lineIn = reader.readLine();
						if (lineIn == null) {
							break;
						}
					}
					mDB.fillRaga(raga);
					if (lineIn == null) {
						return;
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		Window window = getWindow();
		window.setFormat(PixelFormat.RGBA_8888);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		mDB.close();
		mSoundManager.close();
	}

}